using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using System.Collections.Generic;
using Ext.Net;
using Entity;


public partial class admin_sales_ccfail : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindOrders(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindOrders(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<ProductPurchaseOrder> data = null;
        int totalCount = 0;
        string sqlCondition = string.Empty;
        sqlCondition += " OrderStatus ='ccfail'";
        start = start * limit;

        double fromPrice = 0;
        Double.TryParse(txtPriceFrom.Text, out fromPrice);

        double toPrice = 0;
        Double.TryParse(txtPriceTo.Text, out toPrice);

        if (this._useSearch)
        {
            if (txtDateFrom.SelectedDate > DateTime.MinValue)
            {
                sqlCondition += " AND CreateDate>='" + ECommerce.Library.ECommerce.Utility.FormatDateTime(txtDateFrom.SelectedDate, '-') + "'";
            }
            if (txtDateTo.SelectedDate > DateTime.MinValue)
            {
                sqlCondition += " AND CreateDate<='" + ECommerce.Library.ECommerce.Utility.FormatDateTime(txtDateTo.SelectedDate, '-') + "'";
            }

            if (fromPrice > 0.0)
            {
                sqlCondition += " AND OrderPrice >=" + fromPrice.ToString();
            }
            if (toPrice > 0.0)
            {
                sqlCondition += " AND OrderPrice <=" + toPrice.ToString();
            }
            
            if (!string.IsNullOrEmpty(txtFirstName.Text))
            {
                sqlCondition += " AND BillingFirstName LIKE '%" + txtFirstName.Text + "%'";
            }

            if (!string.IsNullOrEmpty(txtEmail.Text))
            {
                sqlCondition += " AND BillingEmailAddress LIKE '%" + txtEmail.Text + "%'";
            }

            this._useSearch = false;
        }

        data = ProductPurchaseOrder.GetBackendProductPurchaseOrderListByCondition(sqlCondition, sort, start, limit);
        totalCount = ProductPurchaseOrder.GetBackendProductPurchaseOrderListByConditionCount(sqlCondition);

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateOrder(int id, string specialInstruction)
    {
        string ModeOfPaymentAccepted = specialInstruction.Trim();
        ProductPurchaseOrder productPurchaseOrder = ProductPurchaseOrder.Load(id);

        if (!string.IsNullOrEmpty(ModeOfPaymentAccepted))
        {
            productPurchaseOrder.OrderStatusMessage = ModeOfPaymentAccepted;
            productPurchaseOrder.OrderStatus = OrderStatus.ManualAuthorization;
        }
        productPurchaseOrder.Update();
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteOrders(string ids)
    {
        //ids = ids.TrimStart(' ');
        //string[] idArray = ids.Split(' ');
        //List<int> idList = new List<int>();
        //foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        //ProductPurchaseOrder.DeleteProductPurchaseOrder(idList);
        //Store1.DataBind();
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray)
        {
            TblProductPurchaseOrder order = TblProductPurchaseOrder.GetTblProductPurchaseOrderByOrderId(Convert.ToInt32(id));
            if (order.GiftCertificateId != null)
            {
                if (order.GiftCertificateId > 0)
                    TblGiftCertificate.DeleteTblGiftCertificate(order.GiftCertificateId);
            }
            TblProductPurchaseOrder_MultipleShippingInfo_Extended.DeleteTblProductPurchaseOrder_MultipleShippingInfo(order.OrderId);
            TblProductPurchaseOrder.DeleteTblProductPurchaseOrder(order.OrderId);
        }
        Store1.DataBind();
        //idList.Add(Convert.ToInt32(id));
        //ProductPurchaseOrder.DeleteProductPurchaseOrder(idList);
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchOrder()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}


